#include "cstring.h"
#include "carray.h"

#include <string.h>
#include <assert.h>

cArray* mallocString()
{
	return mallocArray(sizeof(char));
}

void resetString(cArray* pArray, const char* str)
{
	int lentgh = strlen(str);
	resizeArray(pArray, lentgh + 1);
	pArray->length = lentgh;
	memcpy(pArray->memory, str, lentgh + 1);
}

bool insertToString(cArray* pArray, int index, const char* str)
{
	index = index < 0 ? index : index;
	int length = pArray->length;
	bool isPush = index >= length;
	if (isPush)
	{
		index = length;
	}
	int count = strlen(str);
	if (isPush)
	{
		++count;
	}
	return insertToArray(pArray, index, (void*)str, count, count);
}


void pushToString(cArray* pArray, const char* str)
{
	insertToString(pArray, pArray->length, str);
}


const char& getChar(const cArray* pArray, int index)
{
	void* pValue = getElement(pArray, index);
	assert(pValue);
	return *(char*)pValue;
}


const char* toChar(const cArray* pArray)
{
	if (!pArray)
	{
		return C_NULL_PTR;
	}
	return (const char*)getElement(pArray, 0);
}
